<?php

namespace App\Http\Controllers;

use App\Test;
use ElemeOpenApi\Api\OrderService;
use Illuminate\Http\Request;
use ElemeOpenApi\Config\Config;
use Illuminate\Support\Facades\Cache;


class OrderController extends Controller
{
    /** 获取订单
     * @param $order_id 订单Id
     * @return mixed
     */
    public function getOrder(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
//                $token = Test::find(14)->toArray()['token'];
                $service = new OrderService(json_decode($token),$config);
                $date = $service->get_order($requestData['order_id']);

                    $data[] = [
                        'order_address' => $date->address,
                        'product_name' => $date->createdAt,
                        'product_description' => $date->activeAt,
                        'product_isValid' => $date->deliverFee,
                        'product_parentId' => $date->vipDeliveryFeeDiscount,
                        'product_children' => $date->deliverTime,
                        'order_description' => $date->description,
                        'order_groups' => $date->groups ,
                        'order_invoice' => $date->invoice ,
                        'order_book' => $date->book ,
                        'order_onlinePaid' => $date->onlinePaid ,
                        'order_id' => $date->id ,
                        'order_phoneList' => $date->phoneList ,
                        'order_shopId' => $date->shopId ,
                        'order_openId' => $date->openId ,
                        'order_shopName' => $date->shopName ,
                        'order_daySn' => $date->daySn ,
                        'order_status' => $date->status ,
                        'order_refundStatus' => $date->refundStatus ,
                        'order_userId' => $date->userId ,
                        'order_totalPrice' => $date->totalPrice ,
                        'order_originalPrice' => $date->originalPrice ,
                        'order_consignee' => $date->consignee ,
                        'order_deliveryGeo' => $date->deliveryGeo ,
                        'order_deliveryPoiAddress' => $date->deliveryPoiAddress ,
                        'order_invoiced' => $date->invoiced ,
                        'order_income' => $date->income ,
                        'order_serviceRate' => $date->serviceRate ,
                        'order_serviceFee' => $date->serviceFee ,
                        'order_hongbao' => $date->hongbao ,
                        'order_packageFee' => $date->packageFee ,
                        'order_activityTotal' => $date->activityTotal ,
                        'order_shopPart' => $date->shopPart ,
                        'order_elemePart' => $date->elemePart ,
                        'order_downgraded' => $date->downgraded ,
                        'order_secretPhoneExpireTime' => $date->secretPhoneExpireTime ,
                        'order_orderActivities' => $date->orderActivities ,
                        'order_invoiceType' => $date->invoiceType ,
                        'order_taxpayerId' => $date->taxpayerId ,
                        'order_coldBoxFee' => $date->coldBoxFee ,

                    ];
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 批量获取订单
     * @param $order_ids 订单Id的列表
     * @return mixed
     */
    public function mgetOrders(Request $request)
    {
        $requestData = $request->only([
            'order_ids','type','unique_id'
        ]);

        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
                $service = new OrderService(json_decode($token),$config);
                $dates = $service->mget_orders(json_decode($requestData['order_ids']));
                foreach($dates as $date){
                    $data[] = [
                        'order_address' => $date->address,
                        'product_name' => $date->createdAt,
                        'product_description' => $date->activeAt,
                        'product_isValid' => $date->deliverFee,
                        'product_parentId' => $date->vipDeliveryFeeDiscount,
                        'product_children' => $date->deliverTime,
                        'order_description' => $date->description,
                        'order_groups' => $date->groups ,
                        'order_invoice' => $date->invoice ,
                        'order_book' => $date->book ,
                        'order_onlinePaid' => $date->onlinePaid ,
                        'order_id' => $date->id ,
                        'order_phoneList' => $date->phoneList ,
                        'order_shopId' => $date->shopId ,
                        'order_openId' => $date->openId ,
                        'order_shopName' => $date->shopName ,
                        'order_daySn' => $date->daySn ,
                        'order_status' => $date->status ,
                        'order_refundStatus' => $date->refundStatus ,
                        'order_userId' => $date->userId ,
                        'order_totalPrice' => $date->totalPrice ,
                        'order_originalPrice' => $date->originalPrice ,
                        'order_consignee' => $date->consignee ,
                        'order_deliveryGeo' => $date->deliveryGeo ,
                        'order_deliveryPoiAddress' => $date->deliveryPoiAddress ,
                        'order_invoiced' => $date->invoiced ,
                        'order_income' => $date->income ,
                        'order_serviceRate' => $date->serviceRate ,
                        'order_serviceFee' => $date->serviceFee ,
                        'order_hongbao' => $date->hongbao ,
                        'order_packageFee' => $date->packageFee ,
                        'order_activityTotal' => $date->activityTotal ,
                        'order_shopPart' => $date->shopPart ,
                        'order_elemePart' => $date->elemePart ,
                        'order_downgraded' => $date->downgraded ,
                        'order_secretPhoneExpireTime' => $date->secretPhoneExpireTime ,
                        'order_orderActivities' => $date->orderActivities ,
                        'order_invoiceType' => $date->invoiceType ,
                        'order_taxpayerId' => $date->taxpayerId ,
                        'order_coldBoxFee' => $date->coldBoxFee ,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 确认订单(推荐)
     * @param $order_id 订单Id
     * @return mixed
     */
    public function confirmOrderLite(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
                $service = new OrderService(json_decode($token),$config);
                $service->confirm_order_lite($requestData['order_id']);
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 取消订单(推荐)
     * @param $order_id 订单Id
     * @param $type 取消原因
     * @param $remark 备注说明
     * @return mixed
     */
    public function cancelOrderLite(Request $request)
    {
        $requestData = $request->only([
            'order_id','reason','remark','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
                $service = new OrderService(json_decode($token),$config);
                $service->cancel_order_lite($requestData['order_id'],$requestData['reason'],$requestData['remark']);

                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 同意退单/同意取消单(推荐)
     * @param $order_id 订单Id
     * @return mixed
     */
    public function agreeRefundLite(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
                $service = new OrderService(json_decode($token),$config);
                $service->agree_refund_lite($requestData['order_id']);
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 不同意退单/不同意取消单(推荐)
     * @param $order_id 订单Id
     * @param $reason 商家不同意退单原因
     * @return mixed
     */
    public function disagreeRefundLite(Request $request)
    {
        $requestData = $request->only([
            'order_id','reason','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
                $service = new OrderService(json_decode($token),$config);
                $service->disagree_refund_lite($requestData['order_id'],$requestData['reason']);
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }



    /** 获取订单配送记录                     返回了一个空数组，但是官方文档给的有信息
     * @param $order_id 订单Id
     * @return mixed
     */
    public function getDeliveryStateRecord(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){
                    throw_error('token获取失败');
                });
                $service = new OrderService(json_decode($token),$config);
                $date = $service->get_delivery_state_record($requestData['order_id']);
                    $data[] = [
                        'order_address' => $date->address,
                        'product_name' => $date->createdAt,
                        'product_description' => $date->activeAt,
                        'product_isValid' => $date->deliverFee,
                        'product_parentId' => $date->vipDeliveryFeeDiscount,
                        'product_children' => $date->deliverTime,
                        'order_description' => $date->description,
                        'order_groups' => $date->groups ,
                        'order_invoice' => $date->invoice ,
                        'order_book' => $date->book ,
                        'order_onlinePaid' => $date->onlinePaid ,
                        'order_id' => $date->id ,
                        'order_phoneList' => $date->phoneList ,
                        'order_shopId' => $date->shopId,
                        ];
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }



    /** 订单确认送达(推荐)
     * @param $order_id 订单ID
     * @return mixed
     */
    public function receivedOrderLite(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $service->received_order_lite($requestData['order_id']);
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }


    /** 回复催单                  暂时没办法测试，下单后40分钟之后才可以实行催单功能 ，现在时间2017-09-12 下午16:26
     * @param $remind_id 催单Id
     * @param $type 回复类别
     * @param $content 回复内容,如果type为custom,content必填,回复内容不能超过30个字符
     * @return mixed
     */
    public function replyReminder(Request $request)
    {
        $requestData = $request->only([
            'remind_id','response_type','content','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $dates = $service->reply_reminder($requestData['remind_id'],$requestData['response_type'],$requestData['content']);
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 获取指定订单菜品活动价格.
     * @param $order_id 订单Id
     * @return mixed
     */
    public function getCommodities(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $date = $service->get_commodities($requestData['order_id']);
                $date_array = (Array) $date;
                foreach($date_array as $item){
                    $data[] = [
                        'id'=>$item->id,
                        'userPrice'=>$item->userPrice,
                        'shopPrice'=>$item->shopPrice,
                        'skuId'=>$item->skuId,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }



    /** 批量获取订单菜品活动价格
     * @param $order_ids 订单Id列表
     * @return mixed
     */
    public function mgetCommodities(Request $request)
    {
        $requestData = $request->only([
            'order_ids','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $date = $service->mget_commodities(json_decode($requestData['order_ids']));
                foreach($date as $item){
                    $data[] = [
                        'id'=>$item->id,
                        'userPrice'=>$item->userPrice,
                        'shopPrice'=>$item->shopPrice,
                        'skuId'=>$item->skuId,
                    ];
                }
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }


    /** 获取订单退款信息
     * @param $order_id 订单Id
     * @return mixed
     */
    public function getRefundOrder(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $date = $service->get_refund_order($requestData['order_id']);
                    $data[] = [
                        'refund_orderId' => $date->orderId,
                        'refund_refundStatus' => $date->refundStatus,
                        'refund_refundType' => $date->refundType,
                        'refund_totalPrice' => $date->totalPrice,
                        'refund_goodsList' => $date->goodsList,
                    ];
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }


    /** 批量获取订单退款信息
     * @param $order_ids 订单Id列表
     * @return mixed
     */
    public function mgetRefundOrders(Request $request)
    {
        $requestData = $request->only([
            'order_ids','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $date = $service->mget_refund_orders(json_decode($requestData['order_ids']));
                $data[] = [
                    'refund_orderId' => $date->orderId,
                    'refund_refundStatus' => $date->refundStatus,
                    'refund_refundType' => $date->refundType,
                    'refund_totalPrice' => $date->totalPrice,
                    'refund_goodsList' => $date->goodsList,
                ];
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }




    /** 获取店铺未回复的催单  测试数据为空数组
     * @param $shop_id 店铺id
     * @return mixed
     */
    public function getUnreplyReminders(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $dates = $service->get_unreply_reminders($requestData['shop_id']);
                foreach($dates as $item){
                    $data[] = [
                        'id'=>$item->id,
                        'userPrice'=>$item->userPrice,
                        'shopPrice'=>$item->shopPrice,
                        'skuId'=>$item->skuId,
                    ];
                }
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }



    /** 查询店铺未处理订单
     * @param $shop_id 店铺id
     * @return mixed
     */
    public function getUnprocessOrders(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $data = $service->get_unprocess_orders($requestData['shop_id']);
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }




    /** 查询店铺未处理的取消单        得到的为空数据
     * @param $shop_id 店铺id
     * @return mixed
     */
    public function getCancelOrders(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $data = $service->get_cancel_orders($requestData['shop_id']);
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }

    }


    /** 查询店铺未处理的退单      得到的为空数据
     * @param $shop_id 店铺id
     * @return mixed
     */
    public function getRefundOrders(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $data = $service->get_refund_orders($requestData['shop_id']);
                return $data;
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }



    /** 查询全部订单
     * @param $shop_id 店铺id
     * @param $page_no 页码。取值范围:大于零的整数最大限制为100
     * @param $page_size 每页获取条数。最小值1，最大值50。
     * @param $date 日期,默认当天,格式:yyyy-MM-dd
     * @return mixed
     */
    public function getAllOrders(Request $request)
    {
        $requestData = $request->only([
            'shop_id','page_no','page_size','date','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });
                $service = new OrderService(json_decode($token),$config);
                $dates = $service->get_all_orders($requestData['shop_id'],$requestData['page_no'],$requestData['page_size'],$requestData['date']);
                foreach($dates as $item) {
                    foreach ($item as $date) {
                        $data[] = [
                            'order_address' => $date->address,
                            'order_createdAt' => $date->createdAt,
                            'order_activeAt' => $date->activeAt,
                            'order_deliverFee' => $date->deliverFee,
                            'order_vipDeliveryFeeDiscount' => $date->vipDeliveryFeeDiscount,
                            'order_deliverTime' => $date->deliverTime,
                            'order_description' => $date->description,
                            'order_groups' => $date->groups,
                            'order_invoice' => $date->invoice,
                            'order_book' => $date->book,
                            'order_onlinePaid' => $date->onlinePaid,
                            'order_id' => $date->id,
                            'order_phoneList' => $date->phoneList,
                            'order_shopId' => $date->shopId,
                            'order_openId' => $date->openId,
                            'order_shopName' => $date->shopName,
                            'order_daySn' => $date->daySn,
                            'order_status' => $date->status,
                            'order_refundStatus' => $date->refundStatus,
                            'order_userId' => $date->userId,
                            'order_totalPrice' => $date->totalPrice,
                            'order_originalPrice' => $date->originalPrice,
                            'order_consignee' => $date->consignee,
                            'order_deliveryGeo' => $date->deliveryGeo,
                            'order_deliveryPoiAddress' => $date->deliveryPoiAddress,
                            'order_invoiced' => $date->invoiced,
                            'order_income' => $date->income,
                            'order_serviceRate' => $date->serviceRate,
                            'order_serviceFee' => $date->serviceFee,
                            'order_hongbao' => $date->hongbao,
                            'order_packageFee' => $date->packageFee,
                            'order_activityTotal' => $date->activityTotal,
                            'order_shopPart' => $date->shopPart,
                            'order_elemePart' => $date->elemePart,
                            'order_downgraded' => $date->downgraded,
                            'order_secretPhoneExpireTime' => $date->secretPhoneExpireTime,
                            'order_orderActivities' => $date->orderActivities,
                            'order_invoiceType' => $date->invoiceType,
                            'order_taxpayerId' => $date->taxpayerId,
                            'order_coldBoxFee' => $date->coldBoxFee,
                        ];
                    }
                    return $data;
                }
                break;
            case "Mt":
                break;
            case "Tdd":
                break;
        }
    }








}
